package com.neusoft.edu.smartschool.app.parttern.model;

import android.util.Log;

import com.neusoft.edu.smartschool.app.db.Const;
import com.neusoft.edu.smartschool.app.entity.BaseBean;
import com.neusoft.edu.smartschool.app.entity.UserBean;
import com.neusoft.edu.smartschool.app.nets.RetrofitConfig;
import com.neusoft.edu.smartschool.app.util.AesUtil;
import com.neusoft.edu.smartschool.app.util.RandomUtil;

import org.yeruyi.common.abs.AbsRoyModel;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * @author: yeruyi
 * @date: 2018/5/29
 * @email: yeruyi_roy@outlook.com
 * @modify: 2018/5/29 创建文件
 */
public class LoginModel extends AbsRoyModel {

    public void reqLogin(String stuNum, String stuPwd, final INetCallBack<UserBean> callBack) {
        String aesKey = RandomUtil.generateRandomStr(16);
        try {
            RetrofitConfig
                    .getService()
                    .reqLogin(Const.ConstTestConfig.serveName, AesUtil.encrypt(Const.ConstTestConfig.key, aesKey),
                            AesUtil.encrypt(Const.ConstTestConfig.appid, aesKey), aesKey, stuNum, AesUtil.encrypt(stuPwd, aesKey), ""
                    ).enqueue(new Callback<BaseBean<UserBean>>() {
                @Override
                public void onResponse(Call<BaseBean<UserBean>> call, Response<BaseBean<UserBean>> response) {
                    if (response.code() != 200) {
                        if (response.body().getStatus().equals(Const.RES_STATUS_OK)) {
                            callBack.success(response.body().getData());
                        } else {
                            callBack.failed(response.body().getInfo());
                        }
                    } else {
                        callBack.failed(reqProcessNetCode(response.code()));
                    }
                }

                @Override
                public void onFailure(Call<BaseBean<UserBean>> call, Throwable t) {
                    Log.e(TAG, "reqLogin: 请求登录异常2", t);
                    callBack.failed(t.getMessage());
                }
            });
        } catch (Exception e) {
            Log.e(TAG, "reqLogin: 请求登录异常1", e);
            callBack.failed(e.getMessage());
        }
    }
}
